import router from './router'
// import store from './store'
import { getToken } from '@/utils/auth'

const whiteList = ['/login', '/register', '/forgetPassword'] // 登录的白名单，即不用token即可显示的页面
router.beforeEach((to, from, next) => {
  window.document.title = to.meta.title // 页面标题
  if (getToken()) {
    if (to.path === '/login' || to.path === '/register' || to.path === '/forgetPassword') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    // 在免登录白名单，直接进入
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else { // 否则全部重定向到登录页
      next(`/login?redirect=${to.fullPath}`)
    }
  }
})

router.afterEach(() => {
  console.log('成功跳转')
})
